package main;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
import java.io.File;

/**
 * @param arg1 distpdf
 * @param arg0 srcdoc
 * @since 29.09,2011
 * @version v1.00
 * @author ferdinand
 */
public class Doc2pdf {

    private final Variant False = new Variant(false);

    private final Variant True = new Variant(true);

    /**
     * @param src srcdoc
     * @param dist distpdf
     * @return true convert success
     * @return false convert failed
     */
    public boolean doc2pdf(String src, String dist) {

        String FileFormat = "";

        FileFormat = src.substring(src.length() - 4, src.length());

        if (FileFormat.equalsIgnoreCase(".doc")) {

            ActiveXComponent app = new ActiveXComponent("Word.Application");
            //调用WORD程序的COM组件，启动WORD

            ActiveXComponent distiller = new ActiveXComponent("PDFDistiller.PDFDistiller.1");
            //调用adobe distiller的COM组件

            try {
                app.setProperty("Visible", False);
                //设置word程序非可视化运行

                Dispatch docs = app.getProperty("Documents").toDispatch();
                Dispatch doc = Dispatch.invoke(docs, "Open", Dispatch.Method, new Object[]{src, False, True}, new int[1]).toDispatch();
                //打开WORD文档

                app.setProperty("ActivePrinter", new Variant("Adobe PDF"));

                Variant Background = False;

                Variant Append = False;
                //是否追加打印

                int wdPrintAllDocument = 0;
                //是否打印所有文档

                Variant Range = new Variant(wdPrintAllDocument);

                
                String temppsroute = "e:/temp" + System.currentTimeMillis() + ".ps";
                //输出的postscript临时文件的路径
                System.out.println(temppsroute);

                Variant OutputFileName = new Variant(temppsroute);

                Dispatch.callN((Dispatch) doc, "PrintOut", new Object[]{Background, Append, Range, OutputFileName});
                //调用打印组件打印postscript文件

                Variant outputPDFFilePath = new Variant(dist);

                Variant PDFOption = new Variant("");

                Dispatch.callN(distiller, "FileToPDF", new Object[]{OutputFileName, outputPDFFilePath, PDFOption});
                //调用FileToPDF方法将ps文档转换为pdf文档

                Dispatch.call(doc, "Close", False);
                //关闭WORD文档

                File f = new File(temppsroute);
                if(f.exists())
                    f.delete();
                //删除临时文件
                
            } catch (Exception e) {

                System.out.println(e.getMessage());
                
            } finally {

                app.invoke("Quit", new Variant[]{});
                //退出WORD程序

            }

            return true;
        }

        return false;
    }

    public static void main(String[] args)
    {
        Doc2pdf d2p = new Doc2pdf();
        d2p.doc2pdf("C:/Users/ferdinand/Desktop/wordtopdf/2012AA10093.doc", "C:/Users/ferdinand/Desktop/wordtopdf/2012AA10093.pdf");
    }
}
